/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.st.entity.StMaterialType;
import com.jeesite.modules.st.entity.StUnitSetting;
import com.jeesite.modules.st.service.StMaterialTypeService;
import com.jeesite.modules.st.service.StUnitSettingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.entity.StMaterialRecord;
import com.jeesite.modules.st.service.StMaterialRecordService;

import java.util.List;

/**
 * 物料档案Controller
 *
 * @author HOHD
 * @version 2020-12-16
 */
@Controller
@RequestMapping(value = "${adminPath}/st/stMaterialRecord")
public class StMaterialRecordController extends BaseController {

    @Autowired
    private StMaterialRecordService stMaterialRecordService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public StMaterialRecord get(String id, boolean isNewRecord) {
        return stMaterialRecordService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("st:stMaterialRecord:view")
    @RequestMapping(value = {"list", ""})
    public String list(StMaterialRecord stMaterialRecord, Model model) {
        model.addAttribute("stMaterialRecord", stMaterialRecord);
        return "modules/st/stMaterialRecordList";
    }
    @RequiresPermissions("st:stMaterialRecord:view")
    @RequestMapping(value = {"id"})
    @ResponseBody
    public StMaterialRecord id(StMaterialRecord stMaterialRecord, Model model) {
        return stMaterialRecord;
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("st:stMaterialRecord:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<StMaterialRecord> listData(StMaterialRecord stMaterialRecord, HttpServletRequest request, HttpServletResponse response) {
        stMaterialRecord.setPage(new Page<>(request, response));
        Page<StMaterialRecord> page = stMaterialRecordService.findPage(stMaterialRecord);
        return page;
    }

    @Autowired
    StMaterialTypeService stMaterialTypeService;
    @Autowired
    StUnitSettingService stUnitSettingService;

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("st:stMaterialRecord:view")
    @RequestMapping(value = "form")
    public String form(StMaterialRecord stMaterialRecord, Model model) {
        List<StMaterialType> materialTypeList = stMaterialTypeService.findList(new StMaterialType());
        List<StUnitSetting> listUnitSetting = stUnitSettingService.findList(new StUnitSetting());

        model.addAttribute("listUnitSetting", listUnitSetting);
        model.addAttribute("materialTypeList", materialTypeList);
        model.addAttribute("stMaterialRecord", stMaterialRecord);
        return "modules/st/stMaterialRecordForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("st:stMaterialRecord:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated StMaterialRecord stMaterialRecord) {
        stMaterialRecordService.save(stMaterialRecord);
        return renderResult(Global.TRUE, text("保存物料档案成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("st:stMaterialRecord:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(StMaterialRecord stMaterialRecord) {
        stMaterialRecordService.delete(stMaterialRecord);
        return renderResult(Global.TRUE, text("删除物料档案成功！"));
    }

}